package net.aaa.hotel.dao.impl;

import net.aaa.hotel.dao.HTypeDao;
import net.aaa.hotel.entity.HType;
import net.aaa.hotel.util.BaseDB;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class HTypeDaoImpl implements HTypeDao {
    @Override
    public List<Map> queryAll() {
        String sql="select * from t_type";
        return BaseDB.queryMap(sql);
    }

    @Override
    public int addType(HType ht) {
        if (ht.getId()==null){
            String sql="insert into t_type (name,yj,money,imgpath) values(?,?,?,?)";
            return BaseDB.update(sql,ht.getName(),ht.getYj(),ht.getMoney(),ht.getImgpath());
        }else {
            String sql="update t_type set name=?,yj=?,money=?,imgpath=? where id=?";
            return BaseDB.update(sql,ht.getName(),ht.getYj(),ht.getMoney(),ht.getImgpath(),ht.getId());
        }
    }

    @Override
    public int telType(String gid) {
        String sql = "delete from t_type where id=?";
        return BaseDB.update(sql,gid);
    }

    @Override
    public Map searchType(String gid) {
        String sql="select * from t_type where id=?";
        return BaseDB.queryMap(sql,gid).get(0);

    }

    @Override
    public List<Map> getAllByPage(Integer curr, Integer nums) {
        String sql="select id,name,yj,money from t_type limit ?,?";
        int start = (curr-1)*nums;
        return BaseDB.queryMap(sql,start,nums);
    }

  //  " select count(*) shu from ( " +
    @Override
    public int getCount(String condition) {
        String sql1= " select t.*, count(r.typeid) a from t_room r RIGHT JOIN t_type t ON t.id=r.typeid \n" +
                " where 1=1  ";
        if (condition!=null&&!"null".equals(condition)){
            sql1+=condition;
        }
        String sql=" select count(*) a from ( "+sql1+" GROUP BY t.id ) ss";
        List<Map> maps = BaseDB.queryMap(sql);
        Object a = maps.get(0).get("a");
        int i =Integer.parseInt(a+"");
        return i;
    }

//    List<Map> maps = BaseDB.queryMap(sql);
//    Object a = maps.get(0).get("a");
//    int i = Integer.parseInt(a + "");
//        return i;

    @Override
    public List<Map> getAllByPage(Integer curr, Integer nums, String condition) {
        String sql="select t.*,count(r.typeid) a from t_room r RIGHT JOIN t_type t ON t.id=r.typeid  where 1=1  ";
        if (condition!=null&&!"null".equals(condition)){
           sql+=condition;
        }
        sql+=" GROUP BY t.id LIMIT ?,? ";
        int start = (curr-1)*nums;
        return BaseDB.queryMap(sql,start,nums);
    }


    @Override
    public List<Map> queryAllInfo() {
        String sql="select t.*,count(*) a from t_type t join t_room r on t.id=r.typeid where status!=3 GROUP BY t.id HAVING a>0";
        return BaseDB.queryMap(sql);
    }

    @Override
    public void getNowAndMaxBetweenTime(String format, String s) {
        String sql="{call create_calendar('"+format+"','"+s+"')}";
        BaseDB.getCall(sql);
        //根据插入的时间查询当前的房间
        // return null;
    }

    @Override
    public List<Map> getAllDate() {
        String sql="select * from calendar_custom";
        return BaseDB.queryMap(sql);
    }

    @Override
    public Map getDateOrder(List<Map> mapList, Integer typeid) {

        //房间未满的 时间和剩余的房间数
        List<Map> mapsDate=new ArrayList<>();
        //房间已满的日期
        List manDate=new ArrayList();

        //获取该类型总的房间数
        String sql1="select count(*) a from t_room where `status`!=3 and typeid=?";
        List<Map> maps = BaseDB.queryMap(sql1, typeid);
        int count=Integer.parseInt(maps.get(0).get("a")+"");
        for (Map map : mapList) {
            String sql="{call create_getroomcount('"+map.get("date")+"',"+typeid+",?)}";
            List<String> out = BaseDB.getCallOut(sql, "out");
            //获取的map.get(date)的已经预定或者入住的房间数
            System.out.println("Sehngyude fangjianshu ====="+out.get(0)+"====当时是"+map.get("date"));
            int s = Integer.parseInt(out.get(0));
            //用总的房间数减去s代表是空余的房间数
            int shengyu=count-s;
            System.out.println("剩余的房间数是"+shengyu);
            //将时间   和剩余的房间数  存刀一个map里面
            if(shengyu>0){
                Map m=new HashMap();
                //将时间   和剩余的房间数
                m.put(map.get("date"),shengyu);
                System.out.println("剩余的房间和日期"+m);
                mapsDate.add(m);
            }else{
                //房间已满
                manDate.add(map.get("date"));
            }
        }
        Map ma=new HashMap();
        ma.put("manDate",manDate);
        ma.put("srDate",mapsDate);

        return ma;
    }


    @Override
    public List getManDate(Integer typeid) {
        //获取离开的最大时间
        String sql="call pro_getmaxleaveDate("+typeid+",?)";
        return BaseDB.getCallOut(sql,"out");
    }
}
